package com.shujia.redis;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;

import java.sql.*;

public class RedisCache {

    private Jedis jedis;

    @Before
    public void init() {

        //1、创建链接
        jedis = new Jedis("master", 6379);

    }


    /**
     * 不使用redis版本
     */


    @Test
    public void queryNoRedis() {


        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://master:3306/test", "root", "123456");


            long start = System.currentTimeMillis();

            for (int i = 0; i < 1000; i++) {

                PreparedStatement stat = con.prepareStatement("select * from weibo where id=?");

                stat.setInt(1, 5);

                ResultSet resultSet = stat.executeQuery();

                if (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    String context = resultSet.getString("context");
                    System.out.println(id + "\t" + context);
                }
            }

            long end = System.currentTimeMillis();

            System.out.println(end - start);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用redis做缓存
     */


    @Test
    public void queryOnRedis() {

        long start = System.currentTimeMillis();

        for (int i = 0; i < 1000; i++) {

            String weibId = "1";

            //1、查询redis
            String context = jedis.get("weibo:" + weibId);

            if (context == null) {
                //如果redis中没有这条数据，查询mysql
                String query = query(weibId);

                //将结果保存到redis中， 设置过期时间
                SetParams ex = SetParams.setParams().ex(60);
                jedis.set("weibo:" + weibId, query, ex);

                System.out.println(weibId + "\t" + query);
            } else {
                System.out.println(weibId + "\t" + context);

                //再查询止呕重置过期时间
                jedis.expire("weibo:" + weibId, 60);
            }

        }
        long end = System.currentTimeMillis();

        System.out.println(end - start);
    }

    public static String query(String weibeId) {
        String result = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://master:3306/test", "root", "123456");

            PreparedStatement stat = con.prepareStatement("select * from weibo where id=?");

            stat.setInt(1, Integer.parseInt(weibeId));

            ResultSet resultSet = stat.executeQuery();

            if (resultSet.next()) {
                result = resultSet.getString("context");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result;
    }


    @After
    public void close() {
        jedis.close();
    }

}
